package com.ledim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.bean.LedimUserBean;
import com.ledim.bean.base.BaseResultDataInfo;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.service.coverter.LedimGsonConverterFactory;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.ToastUtil;
import com.ledim.widget.view.LoadingBar;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 修改用户性别
 */
public class UserSexActivity extends LeDimBaseActivity implements View.OnClickListener {

    public static final String USER_SEX = "user_sex";
    public static final int REQUESTCODE_USER_SEX = 301;
    private ImageView mBack;
    private TextView mFinish;
    private LedimUserBean mUser;
    private View mManLayout;
    private View mWomanLayout;
    private ImageView mMan;
    private ImageView mWoman;
    private String mSex;
    private LoadingBar mLoading;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_user_sex;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        mUser = (LedimUserBean) getIntent().getSerializableExtra(USER_SEX);
        mBack = (ImageView) findViewById(R.id.user_sex_back);
        mFinish = (TextView) findViewById(R.id.user_sex_finish);
        mManLayout = findViewById(R.id.user_man_layout);
        mWomanLayout = findViewById(R.id.user_woman_layout);
        mMan = (ImageView) findViewById(R.id.sex_man);
        mWoman = (ImageView) findViewById(R.id.sex_woman);
        mBack.setOnClickListener(this);
        mFinish.setOnClickListener(this);
        mManLayout.setOnClickListener(this);
        mWomanLayout.setOnClickListener(this);
        if (mUser.gender.equals("1")) {
            mSex = "1";
            mMan.setVisibility(View.VISIBLE);
            mWoman.setVisibility(View.GONE);
        } else if (mUser.gender.equals("2")) {
            mSex = "2";
            mMan.setVisibility(View.GONE);
            mWoman.setVisibility(View.VISIBLE);
        } else {
            mSex = "0";
            mMan.setVisibility(View.GONE);
            mWoman.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_sex_back:
                finish();
                break;
            case R.id.user_man_layout:
                mMan.setVisibility(View.VISIBLE);
                mWoman.setVisibility(View.GONE);
                mSex = "1";
                break;
            case R.id.user_woman_layout:
                mMan.setVisibility(View.GONE);
                mWoman.setVisibility(View.VISIBLE);
                mSex = "2";
                break;
            case R.id.user_sex_finish:
                //调用修改姓名的接口
                if (mSex == null) {
                    mSex = "0";
                }
                modifyProfileGender(mSex);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLoading != null) {
            mLoading.dismiss();
        }
    }

    /**
     * 修改用户性别
     */
    private void modifyProfileGender(String sex) {

        if (mLoading == null) {
            mLoading = new LoadingBar(this);
        }
        mLoading.show();
        LedimApiServiceFactory.createService(ApiService.class)
                              .modifyProfileGender(sex)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseResultDataInfo<LedimUserBean>>() {
                                  @Override
                                  public void onCompleted() {
                                      super.onCompleted();
                                  }

                                  @Override
                                  public void onError(Throwable e) {
                                      super.onError(e);
                                      mLoading.dismiss();
                                      ToastUtil.toast(e.getMessage());
                                  }

                                  @Override
                                  public void onNext(BaseResultDataInfo<LedimUserBean> data) {
                                      super.onNext(data);
                                      mLoading.dismiss();
                                      if (data != null) {
                                          if (data.success) {
                                              LedimSpUtils.setUser(LedimGsonConverterFactory.create().gson
                                                      .toJson(data.data));
                                              ToastUtil.toast("用户性别修改成功");
                                              Intent intent = new Intent();
                                              if (mSex.equals("1")) {
                                                  intent.putExtra("content", "男");
                                              } else if (mSex.equals("2")) {
                                                  intent.putExtra("content", "女");
                                              } else {
                                                  intent.putExtra("content", "保密");
                                              }
                                              setResult(REQUESTCODE_USER_SEX, intent);
                                              finish();
                                          } else {
                                              //登录失败
                                              ToastUtil.toast(data.message);
                                          }
                                      }
                                  }
                              });

    }
}
